use std::io::{Write, Read};

use tcp::{server, connection, message, datapack::pack};

fn main() {
    let engine = server::Engine::new("127.0.0.1:8888".into(), 1024, pre_hook, post_hook);
    engine.regist(0, hi);
    engine.run()
}

fn hi(conn:&mut connection::Conn, msg:message::Message) {
    println!("received {:?}", String::from_utf8_lossy(&msg.data.as_slice()));
    conn.rw.write(&[0,1,2]).unwrap();
}

fn pre_hook(conn:&mut connection::Conn) {
    println!("I'm preHook, remote address: {}", conn.rw.peer_addr().unwrap());
}

fn post_hook(conn:&mut connection::Conn) {
    println!("I'm postHook, remote address: {}", conn.rw.peer_addr().unwrap());
}